// Copyright Notice:
//				common.js v1.0.2
//			Copyright@2008-2009 - OpenSiteMobile
//				All rights reserved
// ==========================================================================
//			http://www.opensitemobile.com
// ==========================================================================
// Contact Information:
//			Author: Dwight Vietzke
//			Email:  dwight_vietzke@yahoo.com

/*
	OpenSiteMobile common dojo integrated functions
*/

dojo.provide("msos.common");


// --------------------------
// Document Type Declaration
// --------------------------

msos.common.xml = '<?xml version="1.0" encoding="utf-8"?>' + "\n";

msos.common.doctype = {
        xhtml_strict : '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
        xhtml_MP12 :   '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">',
        xhtml_trans :  '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
}

for (var key in msos.common.doctype) {
        msos.common.doctype[key] += "\n" + '<html xmlns="http://www.w3.org/1999/xhtml">' + "\n";
}

msos.common.content = {
        xml :   '<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />' + "\n",
        xhtml : '<meta http-equiv="content-type" content="text/html; charset=utf-8" />' + "\n"
}


// --------------------------
// Browser detection
// --------------------------

msos.common.browser_current = function() {
        var failed = [];
	if (!window.focus)              failed.push('window.focus');
	if (!document.open)             failed.push('document.open');
        if (!document.write)            failed.push('document.write');
	if (!document.close)            failed.push('document.close');
	if (!document.images)           failed.push('document.images');
	if (!document.styleSheets)      failed.push('document.styleSheets');
        if (failed.length == 0) {
                console.debug('browser_current -> Browser is current');
                return true;
        } else {
                console.debug('browser_current -> Browser does not support: ' + failed.join(', ') + ' -> for content type ' + djConfig.msos_content);
                if (djConfig.msos_content == 'xml'
                 && window.focus
                 && document.images
                 && document.styleSheets) return true;
          }
        console.warn('browser_current -> Browser is not current');
        return false;
}

msos.common.browser_editable = function() {
        if (!(document.designMode || document.body.contentEditable))    { return false; }
        if (typeof(document.execCommand) == 'undefined')                { return false; }
        console.debug('browser_editable -> Browser supports content editing');
        return true;
}


// --------------------------
// CSS helper functions
// --------------------------

msos.common.get_active_css = function () {
        var i = 0, a, els = dojo.query("link");
        a = els[i];
	while (a) {
		if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) {
			return a.getAttribute("title");
		}
                a = els[i++];
	}
	return null;
}


// --------------------------
// Create Popup Window
// --------------------------

msos.common.open_popup_window = function(win_type, win_name, page_title, page_desc, page_text, page_style, page_src, win_width, win_height, features, win_url) {
	var pop_note = 'Popup window failed!';
	var pop_win = null;
	var win_size = { width:0, height:0 };
        var window_w = win_width || 218;
        var window_h = win_height || 272;
        var params = features || [];
        var window_url = win_url || '';
	var close_pop_win = function() { return true; }
	var flag = false;

        if (window_w < 100) { window_w = 100; }
        if (window_h < 100) { window_h = 100; }

        if (params[0]) console.debug(params);

	// Width and height set to 218, 272 respectively, for default size window
	var window_params = "width=" + window_w + ",height=" + window_h + ",left=2,top=2,dependent=1,status=0,toolbar=0,menubar=0," +
                            "scrollbars=0,personalbar=0,resizable=0,location=0,directories=0,hotkeys=0";

	for (var j = 0; j < params.length; j++) {
		var par_no  = params[j] + '=0';
		var par_yes = params[j] + '=1';
                var pattern = new RegExp(par_no, 'i');
		if (pattern.test(window_params)) { window_params = window_params.replace(pattern, par_yes);  }
                else                             { window_params = window_params + "," + par_yes;            }
	}

	pop_win = window.open(window_url, win_name, window_params, true) || null;

	if (pop_win && !pop_win.closed) {
		if (msos.common.var_is_null(pop_win.document))	{ djConfig.msos_popups_blocked = true; }
		if (msos.common.var_is_null(pop_win.closed))	{ djConfig.msos_popups_blocked = true; }
	} else							{ djConfig.msos_popups_blocked = true; }

	if (djConfig.msos_popups_blocked) {
		pop_win = null;
		console.warn('open_popup_window -> Popup window failed or blocked!');
	}

	if (pop_win) {
		var pop_page = '';
		var pop_content = '';
		var pop_buttons = '';
                var pop_doctype = djConfig.msos_doctype;

		if        (win_type == 'image') {
			pop_content = '<img src="' + page_src + '" alt="' + page_title + '" />';
			pop_buttons = '<a href="' + page_src + '"><b>' + page_text + '</b></a>';
		} else if (win_type == 'iframe') {
			pop_content = '<iframe id="popup_iframe" src="' + page_src + '"></iframe>';
			pop_buttons = '<a href="' + page_src + '"><b>' + page_text + '</b></a>';
                        pop_doctype = 'xhtml_trans';   // iframe not available in MP 1.2 or strict spec.
		} else if (win_type == 'debug') {
			pop_content = '<pre id="popup_debug_el">' + page_text + '</pre>';
			pop_buttons = '<a href="' + page_src + '"><b>' + page_src + '</b></a>';
		} else if (win_type == 'div') {
			pop_content = "Add content using 'popup_content' element and innerHTML";
			pop_buttons = '<a href="' + page_src + '"><b>' + page_text + '</b></a>';
		} else if (win_type == 'url') {
                        return pop_win;
                } else {
                        console.warn('open_popup_window - No window type specified!');
                        return null;
                }

                if (djConfig.msos_content == 'xml') {
                        pop_page = msos.common.xml;
                }
		pop_page += msos.common.doctype[pop_doctype];
                pop_page += "<head>\n";
                pop_page += msos.common.content[djConfig.msos_content];
		pop_page += "<title>" + page_title + "</title>\n";
		pop_page += page_style + "\n";
                pop_page += "</head>\n<body>\n";
		pop_page += "<div id='popup_title'>"	 + page_desc   + "</div>\n";
		pop_page += "<div id='popup_content'>\n" + pop_content + "</div>\n";
		pop_page += "<div id='popup_buttons'>\n" + pop_buttons + "</div>\n";
		pop_page += "</body>\n</html>";

		pop_note = 'Popup window created -> ' + win_type;

		close_pop_win = function() { if (pop_win && !pop_win.closed) pop_win.close(); }

		var pop_d = pop_win.document;
		pop_d.open();
		pop_d.write(pop_page);
		pop_d.close();
                pop_win.focus();

		if (win_type == 'debug') {
			console.info(pop_note);
		} else {
			dojo.connect(window, 'onfocus', null, close_pop_win);
			if (djConfig.isDebug) {
				console.debug(pop_note);
				var temp_array = window_params.split(",");
				window_params = temp_array.join(", ");
				console.info('Popup window parameters: ' + window_params);
				win_size = msos.common.getViewport(pop_win);
				console.info('Actual window size: Width: ' + win_size.width + ' Height: ' + win_size.height);
			}
		  }
	}
        return pop_win;
}


// --------------------------
// Dialog Window from URL: parent page
// --------------------------

msos.common.open_dialog_window = function(main_obj, url, win_name, win_width, win_height, action, init_params, req_params, valid_func, cancel_func, win_features) {
        var features = win_features || [];
        var popup_dialog = {
                _main_obj : main_obj,
                _action : action,
                _arguments : init_params,
                _required : req_params || null,
                _validate : valid_func || null,
                _cancel : cancel_func  || null
        };

        var dlg = msos.common.open_popup_window('url',  win_name, '', '', '', '', '', win_width, win_height, features, url);
	if (dlg) { dlg.dialog = popup_dialog; }
        window.pop_dialog = popup_dialog;       // Store this on original page for Opera
	return dlg;
}


// --------------------------
// Dialog Window Popup: child page
// --------------------------

msos.common.init_popup = function(win) {
	var pop_doc = win.document;
	var pop_obj = this;

	this.popup_cancel = function() {
		msos.common.popup_cancel(win);
	}

	this.init_fields = function() {

		if (!win.dialog) {
                        if (win.opener && win.opener.pop_dialog) {
                                // Opera v9.6+ has a timing, blocking issue, so try this
                                win.dialog = win.opener.pop_dialog;
                        }
		}

		if (!win.dialog) {
			if (win.opener) win.opener.console.warn('init_fields -> Failed: missing dialog object');
                        msos.common.popup_cancel(win);
                        return false;
		}

		var display_inputs = "Input Values:\n";
		var inp_par = win.dialog._arguments || {};

		for (var field in inp_par) {
			display_inputs += field + ' -> ' + inp_par[field] + "\n";
			var form_el = dojo.byId(field, pop_doc) || null;
			if (form_el) {
				var tag_name = form_el.tagName.toLowerCase();
				if	(tag_name == 'div')	{ form_el.innerHTML = inp_par[field]; }
				else if	(tag_name == 'select')	{ msos.common.set_select_menu(form_el, inp_par[field]);	}
				else				{ form_el.value = inp_par[field]; }
			} else {
				display_inputs += "Error: missing\ninput element? -> " + field + "\n";
			  }
		}
		msos.common.debug_write(display_inputs);
		win.opener.console.debug('init_popup - init_fields -> Success!');
		return true;
	}

	this.popup_action = function() {
		var display_outputs = "Raw data output:\n";

		if (!win.dialog) {
			win.opener.console.warn('init_popup - popup_action -> Failed: missing dialog object');
			msos.common.popup_cancel(win);
			return false;
		}

		var out_param = new Object();
		var inp_param = win.dialog._arguments	|| {};
		var required  = win.dialog._required	|| {};
		var validate = win.dialog._validate	||
			function(inp_elm, field_name, o_parm_obj) { o_parm_obj[field_name] = inp_elm.value || ''; };

		for (var i in required) {
			var el = dojo.byId(i, pop_doc) || null;
			if (el && !el.value) { alert(required[i]);  el.focus();  return false; }
		}
		for (var field in inp_param) {
			var el = dojo.byId(field, pop_doc) || null;
			if (el) {
				// Run validation function on current input field values
				validate(el, field, out_param);
				display_outputs += field + ' -> ' + out_param[field] + "\n";
			} else {
				win.opener.console.warn('init_popup - popup_action -> Setting popup action failed - field: ' + field);
				msos.common.popup_cancel(win);
				return false;
			  }
		}
		// We only send back values that have changed...
		display_outputs += "Final Values:\n";
		for (var field in inp_param) {
			if (inp_param[field] == out_param[field]) out_param[field] = '';
			display_outputs += field + ' -> ' + out_param[field] + "\n";
		}
		msos.common.debug_write(display_outputs);
		win.dialog._action(out_param);
		return true;
	}
}

msos.common.popup_cancel = function(win_cnl) {
	var cancel = null;
	if (win_cnl && !win_cnl.closed) {
		if (win_cnl.dialog) cancel = win_cnl.dialog._cancel || null;
		if (cancel) cancel();
		win_cnl.close();
		win_cnl = null;
		return false;
	}
	return true;
}


// --------------------------
// Popup Div Functions
// --------------------------

msos.common.set_popup_position = function(display_elm, ref_elm, pars) {
	var cs = dojo.getComputedStyle(display_elm);
	var pos = cs.position;
	// Use the position of a reference element, if given
	var target_elm = ref_elm || display_elm;
	if (pos != "absolute") display_elm.style.position = "absolute";
	var offset = dojo._abs(target_elm, true);
	var adjust_left = pars.adj_left || 0;
	var adjust_top  = pars.adj_top  || 0;
	console.debug("  Param pos (rel): top -> " + adjust_top + ', left -> ' + adjust_left);

	adjust_left = adjust_left + offset.x;
	adjust_top  = adjust_top  + offset.y;
	console.debug("  Input pos (abs): top -> " + offset.y   + ', left -> ' + offset.x);
	console.debug("Display pos (abs): top -> " + adjust_top + ', left -> ' + adjust_left);
	display_elm.style.left = adjust_left + 'px';
	display_elm.style.top  = adjust_top  + 'px';
}

msos.common.set_suppress_ie_objects = function(popup_elm, objs_to_suppress, suppression_list) {
	// This is only necessary for IE
	if(!document.all) { return; }
	// Now do all elements in 'objs_to_suppress' list
	for(var k = 0; k < objs_to_suppress.length; k++) {
		var tag_array = document.getElementsByTagName(objs_to_suppress[k].toUpperCase());
		var sup_left;
		var sup_top;
		var sup_height;
		var sup_width;
		for(var i=0; i<tag_array.length; i++) {
			var element = tag_array[i];
			if(!element || !element.offsetParent) continue;
			var offset_element = element,
			sup_left = 0,
			sup_top  = 0,
			sup_height = offset_element.offsetHeight,
			sup_width  = offset_element.offsetWidth;
				
			while(offset_element.tagName!="BODY"){
				sup_left += offset_element.offsetLeft; sup_top += offset_element.offsetTop; offset_element = offset_element.offsetParent;
			}
			if(!((popup_elm.offsetLeft + popup_elm.offsetWidth)  <= sup_left)
			&& !((popup_elm.offsetTop  + popup_elm.offsetHeight) <= sup_top ) 
			&& !( popup_elm.offsetTop  >= (sup_top  + sup_height)) 
			&& !( popup_elm.offsetLeft >= (sup_left + sup_width))) {
				if(element.style.visibility == "" || element.style.visibility == "visible") {
					suppression_list.push(element);
				}
			}
		}
	}
}


// --------------------------
// DHTML Functions
// --------------------------

msos.common.switch_img_onmouseover = function(targ_img, orig_img, over_img) {
	if (!targ_img || !targ_img.src) {
                console.error('switch_img_onmouseover - No image specified!');
                return;
        }
	var pattern = new RegExp(orig_img);
	var orig_src = targ_img.src;
	var over_src = orig_src.replace(pattern, over_img);
	dojo.connect(targ_img, 'onmouseover', null,
		function (evt) {
			targ_img.src = over_src;
			var temp_txt = "\nMouseover URI -> \n" + targ_img.src;
			msos.common.event_debug(evt, temp_txt);
		});
	dojo.connect(targ_img, 'onmouseout', null,
		function (evt) {
			targ_img.src = orig_src;
			var temp_txt = "\nMouseout URI -> \n" + targ_img.src;
			msos.common.event_debug(evt, temp_txt);
		});
	if (orig_src == over_src) console.error('switch_img_onmouseover - Switch Image failed!');
}


// --------------------------
// Image Button generation
// --------------------------

msos.common.generate_img_button = function(parent_elm) {
	this.button_mousedown	= null;
	this.button_mouseup	= null;
	this.button_onclick	= null;
	this.img_elm		= null;
	this.img_url		= djConfig.msos_folder + '/images/';
	this.img_name		= 'missing.gif';
	this.img_title		= 'Click here!';
	this.img_alt		= 'Tool';
	this.img_size		= 'img_reg';
	this.img_but_id		= 'button';
	this.but_style_on	= 'button_on';
	this.but_style_off	= 'button_off';
	this.but_style_hov	= 'button_hover';
	this.but_style_prs	= 'button_press';
	var gen_but = this;

	// Set/update function for the state of an interactive element...
	this.set_state = function(id, new_value) {
		var old_value = this[id];
		var elm = this.element;
		if (old_value != new_value) {
			switch (id) {
				case "enabled":
				if (new_value)	{ dojo.removeClass(elm, gen_but.but_style_off); }
				else		{    dojo.addClass(elm, gen_but.but_style_off); }
				break;
				case "active":
				if (new_value)	{    dojo.addClass(elm, gen_but.but_style_prs); }
				else		{ dojo.removeClass(elm, gen_but.but_style_prs); }
				break;
			}
			this[id] = new_value;
		}
	}

	this.generate_img = function() {
		var img_button =	document.createElement("img");
		var image_src =		gen_but.img_url + gen_but.img_name;
		img_button.src =	image_src;
		img_button.title =	gen_but.img_title;
		img_button.alt =	gen_but.img_alt;
		img_button.className =	gen_but.img_size;
		img_button.unselectable = "on";
		// Add a generic image button object for programmable control
		img_button.button = {
			name    : gen_but.img_but_id,		// the button program id (i.e. 'bold')
			element	: img_button,			// the image element
			enabled : true,				// is it enabled?
			active  : false,			// is it pressed?
			text	: false,			// enabled in text mode (ref. xhtml editor)?
			cmd	: gen_but.button_onclick,	// function ref.
			state	: gen_but.set_state,		// for changing state
			context	: null,				// enabled in a certain context?
			tool	: null				// tool instance this button was created for
		}
		return img_button;
	}

	this.img_button_append = function() {
		var img_el = gen_but.generate_img();
		// Add button 'visual' events
		dojo.connect(img_el, 'onmouseover', null,
			function (evt) {
				if (img_el.button.enabled) { dojo.addClass(img_el, gen_but.but_style_hov); }
				var temp_txt = msos.common.gen_input_debug(img_el, img_el.button, 'mouse over');
				msos.common.event_debug(evt, temp_txt);
			});
		dojo.connect(img_el, 'onmouseout', null,
			function (evt) {
				if (img_el.button.enabled) {
					dojo.removeClass(img_el, gen_but.but_style_hov);
					dojo.removeClass(img_el, gen_but.but_style_on);
					(img_el.button.active) && dojo.addClass(img_el, gen_but.but_style_prs);
				}
				var temp_txt = msos.common.gen_input_debug(img_el, img_el.button, 'mouse out')
				msos.common.event_debug(evt, temp_txt);
			});
		// Add mouse driven events
		dojo.connect(img_el, "onmousedown", null,
			function (evt) {
				evt.preventDefault();
				evt.stopPropagation();
				if (img_el.button.enabled) {
					   dojo.addClass(img_el, gen_but.but_style_on);
					dojo.removeClass(img_el, gen_but.but_style_prs);
					if (gen_but.button_mousedown) { gen_but.button_mousedown(); }
				}
				var temp_txt = msos.common.gen_input_debug(img_el, img_el.button, 'mouse down');
				msos.common.event_debug(evt, temp_txt);
			});
		dojo.connect(img_el, "onmouseup", null,
			function (evt) {
				evt.preventDefault();
				evt.stopPropagation();
				if (img_el.button.enabled) {
					dojo.removeClass(img_el, gen_but.but_style_on);
					dojo.removeClass(img_el, gen_but.but_style_hov);
					if (gen_but.button_mouseup) { gen_but.button_mouseup(); }
				}
				var temp_txt = msos.common.gen_input_debug(img_el, img_el.button, 'mouse up');
				msos.common.event_debug(evt, temp_txt);
			});
		dojo.connect(img_el, "onclick", null,
			function (evt) {
				evt.preventDefault();
				evt.stopPropagation();
				if (img_el.button.enabled && img_el.button.cmd) {
					img_el.button.cmd(img_el.button);
				}
			});
		gen_but.img_elm = img_el;
		parent_elm.appendChild(img_el);
	}
}


// --------------------------
// Helper Functions
// --------------------------

msos.common.getViewport = function (win) {
	// Same code as dojo.html.getViewport (Dojo v0.4.2) but works
	// for any window (ie: popups, iframes)
	var _window = win;
	var _document = win.document;
	var w = 0;
	var h = 0;
	if (dojo.isMozilla) {
		w = _window.innerWidth;
		h = _window.innerHeight;
	} else {
		if (!dojo.isOpera && _window.innerWidth) {
			w = _window.innerWidth;
			h = _window.innerHeight;
		} else {
			if (!dojo.isOpera && dojo.exists(_document, "documentElement.clientWidth")) {
				var w2 = _document.documentElement.clientWidth;
				if (!w || w2 && w2 < w) {
					w = w2;
				}
				h = _document.documentElement.clientHeight;
			} else {
				if (_document.body.clientWidth) {
					w = _document.body.clientWidth;
					h = _document.body.clientHeight;
				}
			}
		}
	}
	return {width:w, height:h};
}

msos.common.dec_to_hex = function(num) {
	var mult = parseInt(num / 16);
	var base = msos.common.dec_eq_hex(mult);
	var remainder = num - (mult * 16);
	return base + '' + msos.common.dec_eq_hex(remainder);
}

msos.common.dec_eq_hex = function(num) {
	if (num <= 9) return num;
	switch (num) {
		case 10:	return 'A';
		case 11:	return 'B';
		case 12:	return 'C';
		case 13:	return 'D';
		case 14:	return 'E';
		case 15:	return 'F';
	}
	return 0;
}

msos.common.hex_to_dec = function(hex) {
	if (hex.length == 6) {
		return	  msos.common.hex_to_dec(hex.substr(0,2)) + ' '
			+ msos.common.hex_to_dec(hex.substr(2,2)) + ' '
			+ msos.common.hex_to_dec(hex.substr(4,2));
	}
	var mult = parseInt(msos.common.hex_eq_dec(hex.substr(0,1)));
	var sing = parseInt(msos.common.hex_eq_dec(hex.substr(1)));
	return (mult * 16) + sing;
}

msos.common.hex_eq_dec = function(num) {
	if (num <= 9) return num;
	switch (num.toUpperCase()) {
		case 'A':	return 10;
		case 'B':	return 11;
		case 'C':	return 12;
		case 'D':	return 13;
		case 'E':	return 14;
		case 'F':	return 15;
	}
	return 0;
}

msos.common.xml_innerhtml = function(elm, html) {
        html = msos.common.escape_xml(html);
        if (djConfig.debugAtAllCost) console.debug('xml_innerhtml -> ' + html);
        elm.innerHTML = html;
}

msos.common.escape_xml = function (str) {
        // Almost same code as 'dojo.string.escapeXml' from dojo.string.extras, Dojo v0.4.2
	str = str.replace(/&/gm, "&amp;").replace(/</gm, "&lt;").replace(/>/gm, "&gt;").replace(/"/gm, "&quot;");
	return str;
}

msos.common.escape_string = function (str) {
        // Same code as 'dojo.string.escapeString' from dojo.string.extras, Dojo v0.4.2
	return ("\"" + str.replace(/(["\\])/g, "\\$1") + "\"").replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n").replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r");
}

msos.common.var_is_null = function(variable) {
	if (typeof(variable) == 'undefined')	return true;
	if (variable == null)			return true;
	return false;
}

msos.common.var_is_empty = function(variable) {
	if (msos.common.var_is_null(variable))	return true;
	if (variable == '')			return true;
	return false;
}

msos.common.gen_input_debug = function (elm, elm_prog_obj, event_name) {
	var txt =
		"\nGenerate button - "	+ event_name
		+ "\nButton id -> "	+ elm_prog_obj.name
		+ "\nEnabled -> "	+ elm_prog_obj.enabled
		+ "\nActive -> "	+ elm_prog_obj.active
		+ "\nCurrent button class:\n" + elm.className;
	return txt;
}

msos.common.remove_children = function(node) {
        var number = node.childNodes.length || 0;
        var removed = 0;
        while(node.hasChildNodes()) { node.removeChild(node.firstChild);  ++removed; }
        console.debug('remove_children -> nodes removed: ' + removed + ' of ' + number);
}

msos.common.set_select_menu = function(select_el, match) {
	var opts = select_el.options || [];
	var flag = false;
	if (!match) {
		opts[0].selected = true;
		flag = true;
	}
	for (var i in opts) {
		if(!opts[i]) continue;		// Required by IE 8.0 for some reason
		if (opts[i].value == match) {
			opts[i].selected = true;
			flag = true;
		}
	}
	if (!flag) {
		var opt = document.createElement("option");
		opt.value = match;
		opt.innerHTML = match;
		select_el.appendChild(opt);
		opt.selected = true;
	}
}


// --------------------------
// Debugging stuff
// --------------------------

// Note: Many msos.common scripts use debugging independently
//	 (including previously opened windows) so...
msos.common.debug_window  = null;
msos.common.debug_element = null;

if (window.opener) {
	msos.common.debug_window  = window.opener.msos.common.debug_window  || null;
	msos.common.debug_element = window.opener.msos.common.debug_element || null;
}

msos.common.event_debug = function(evt, text) {
        if (!djConfig.msos_debug) return;
	var txt = 'no event';
	var tag = 'na';
	if (evt) {
		if (evt.type)	txt  = evt.type;
		else		txt  = 'no type';
		if (evt.target) {
				tag  = evt.target.nodeName.toLowerCase();
				if (evt.target.id) tag += ' - ' + evt.target.id;
		}
		else		tag  = 'no target';
		if (text)	tag += text; 
	}
	txt = "Event = " + txt + "\nEvent target tag = " + tag;

        msos.common.debug_write(txt);
        if (djConfig.debugAtAllCost) console.debug(evt);
}

msos.common.debug_write = function(text) {
        if (!djConfig.msos_debug) return;
	if ( msos.common.debug_window
	 &&  msos.common.debug_element
	 && !msos.common.debug_window.closed) {
		if (dojo.isIE)  msos.common.debug_element.innerText = text;
		else	        msos.common.debug_element.innerHTML = text;
	}
}

if(djConfig.msos_debug && (!msos.common.debug_element || !msos.common.debug_window)) {
	msos.common.debug_window = msos.common.open_popup_window('debug', 'msos_debug_window', 'Debug Window', 'Javascript debug output', 'Debug output appears here!', djConfig.msos_style, 'http://www.opensitemobile.com');
	if (msos.common.debug_window) {
                var debug_element_set = function() {
                        msos.common.debug_element = dojo.byId('popup_debug_el', msos.common.debug_window.document);
                }
                setTimeout(debug_element_set, 3000);
        }
}

